﻿using AutoMapper;
using MediatR;
using SmartTracking.API.Write.Application.Commands.Role_command;
using SmartTracking.Domain.UserInfo;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.Role_handler
{
    public class UpdateRoleCommandHandler:IRequestHandler<UpdateRoleCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<Roles> roleRep;
        private readonly IBaseRepository<RolePermissionsMiddle> rolePermissionRep;
        private readonly IMapper mapper;

        public UpdateRoleCommandHandler(
            IBaseRepository<Roles> roleRep,
            IBaseRepository<RolePermissionsMiddle> rolePermissionRep,
            IMapper mapper)
        {
            this.roleRep = roleRep;
            this.rolePermissionRep = rolePermissionRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.更新成功;
            result.Message = ApiEnum.更新成功.ToString();

            using (var tran = roleRep.DB.Database.BeginTransaction())
            {
                try
                {
                    #region 更新角色信息
                    Roles roles = mapper.Map<Roles>(request);
                    roles.UpdateTime = DateTime.Now;
                    var list = await roleRep.UpdateAsync(roles);
                    #endregion

                    #region 修改角色权限
                    List<RolePermissionsMiddle> rolePermission = new List<RolePermissionsMiddle>();
                    var roleper = rolePermissionRep.GetAll().Where(x => x.RoleId == request.RoleId).ToList();
                    foreach (var item in roleper)
                    {
                        roleper.Remove(new RolePermissionsMiddle()
                        {
                            RoleId = roles.RoleId,
                            PermissionId = item.PermissionId
                        });
                    }
                    await rolePermissionRep.DeleteRangeAsync(roleper);
                    foreach (var item in request.PermissionId)
                    {
                        rolePermission.Add(new RolePermissionsMiddle()
                        {
                            RoleId = roles.RoleId,
                            PermissionId = item
                        });
                    }
                    await rolePermissionRep.AddRangeAsync(rolePermission);
                    #endregion

                    tran.Commit();
                    result.data = list;
                }
                catch (Exception ex) 
                {
                    result.Code = ApiEnum.更新失败;
                    result.Message = ApiEnum.更新失败.ToString()+","+ ex.Message;
                    tran.Rollback();
                }
            }
            return result;
        }
    }
}
